<HTML>
  <HEAD>
    <TITLE>threadpool Documentation</TITLE>
    <LINK HREF="threadpool.css" REL="stylesheet" TYPE="text/css">
  </HEAD>
  <BODY BGCOLOR="#FFFFFF">

    <DIV CLASS="qindex">
      <TABLE BORDER="0" WIDTH="100%">
        <TR>

          <TD ALIGN="LEFT">
            <B>threadpool</B>
          </TD>
          <TD ALIGN="RIGHT">
            <A CLASS="qindex" HREF="../index.html">Overview</A> |
            <A CLASS="qindex" HREF="../reference/annotated.html">Reference</A> |
            <A CLASS="qindex" HREF="../tutorial/intro.html">Tutorial</A> |
            <A CLASS="qindex" HREF="../examples/intro.html">Examples</A> |
            <A CLASS="qindex" HREF="../design/intro.html">Design</A>
          </TD>

        </TR>
      </TABLE>
    </DIV>
    <DIV CLASS="qindex">
      <TABLE BORDER="0" WIDTH="100%">
        <TR>
          <TD ALIGN="LEFT">
            <B>Reference</B>
          </TD>
          <TD ALIGN="RIGHT">
            <A CLASS="qindex" HREF="annotated.html">Class List</A> |
            <A CLASS="qindex" HREF="functions.html">Member Functions</A> |
            <A CLASS="qindex" HREF="namespacemembers.html">Typedefs</A> |
            <A CLASS="qindex" HREF="files.html">File List</A>
          </TD>
        </TR>
      </TABLE>
    </DIV>
<!-- Generated by Doxygen 1.4.6-NO -->
<h1>threadpool/task_adaptors.hpp</h1><a href="a00034.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 
<a name="l00017"></a>00017 <span class="preprocessor">#ifndef THREADPOOL_TASK_ADAPTERS_HPP_INCLUDED</span>
<a name="l00018"></a>00018 <span class="preprocessor"></span><span class="preprocessor">#define THREADPOOL_TASK_ADAPTERS_HPP_INCLUDED</span>
<a name="l00019"></a>00019 <span class="preprocessor"></span>
<a name="l00020"></a>00020 
<a name="l00021"></a>00021 <span class="preprocessor">#include &lt;boost/smart_ptr.hpp&gt;</span>
<a name="l00022"></a>00022 <span class="preprocessor">#include &lt;boost/function.hpp&gt;</span>
<a name="l00023"></a>00023 <span class="preprocessor">#include &lt;boost/thread.hpp&gt;</span>
<a name="l00024"></a>00024 
<a name="l00025"></a>00025 
<a name="l00026"></a>00026 <span class="keyword">namespace </span>boost { <span class="keyword">namespace </span>threadpool
<a name="l00027"></a>00027 {
<a name="l00028"></a>00028 
<a name="l00037"></a><a class="code" href="a00036.html#c6f15485d0c9225572afc57b30f50358">00037</a>   <span class="keyword">typedef</span> function0&lt;void&gt; <a class="code" href="a00036.html#c6f15485d0c9225572afc57b30f50358">task_func</a>;
<a name="l00038"></a>00038 
<a name="l00039"></a>00039 
<a name="l00040"></a>00040 
<a name="l00041"></a>00041 
<a name="l00051"></a><a class="code" href="a00021.html">00051</a>   <span class="keyword">class </span><a class="code" href="a00021.html">prio_task_func</a>
<a name="l00052"></a>00052   {
<a name="l00053"></a>00053   <span class="keyword">private</span>:
<a name="l00054"></a>00054     <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> m_priority;  
<a name="l00055"></a>00055     <a class="code" href="a00036.html#c6f15485d0c9225572afc57b30f50358">task_func</a> m_function;     
<a name="l00056"></a>00056 
<a name="l00057"></a>00057   <span class="keyword">public</span>:
<a name="l00058"></a><a class="code" href="a00021.html#a7977071f7d2aed3b105ef9681553578">00058</a>     <span class="keyword">typedef</span> <span class="keywordtype">void</span> <a class="code" href="a00021.html#a7977071f7d2aed3b105ef9681553578">result_type</a>; 
<a name="l00059"></a>00059 
<a name="l00060"></a>00060   <span class="keyword">public</span>:
<a name="l00065"></a><a class="code" href="a00021.html#4223c1c97c5e6fb942f7058f0e80ceba">00065</a>     <a class="code" href="a00021.html#4223c1c97c5e6fb942f7058f0e80ceba">prio_task_func</a>(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <span class="keyword">const</span> priority, <a class="code" href="a00036.html#c6f15485d0c9225572afc57b30f50358">task_func</a> <span class="keyword">const</span> &amp; function)
<a name="l00066"></a>00066       : m_priority(priority)
<a name="l00067"></a>00067       , m_function(function)
<a name="l00068"></a>00068     {
<a name="l00069"></a>00069     }
<a name="l00070"></a>00070 
<a name="l00073"></a><a class="code" href="a00021.html#79168ecc681deee0ed004d10176c8bfe">00073</a>     <span class="keywordtype">void</span> <a class="code" href="a00021.html#79168ecc681deee0ed004d10176c8bfe">operator() </a>(<span class="keywordtype">void</span>)<span class="keyword"> const</span>
<a name="l00074"></a>00074 <span class="keyword">    </span>{
<a name="l00075"></a>00075       <span class="keywordflow">if</span>(m_function)
<a name="l00076"></a>00076       {
<a name="l00077"></a>00077         m_function();
<a name="l00078"></a>00078       }
<a name="l00079"></a>00079     }
<a name="l00080"></a>00080 
<a name="l00085"></a><a class="code" href="a00021.html#1c28c491e589e6b3de2b21e66a1f8544">00085</a>     <span class="keywordtype">bool</span> <a class="code" href="a00021.html#1c28c491e589e6b3de2b21e66a1f8544">operator&lt; </a>(<span class="keyword">const</span> <a class="code" href="a00021.html">prio_task_func</a>&amp; rhs)<span class="keyword"> const</span>
<a name="l00086"></a>00086 <span class="keyword">    </span>{
<a name="l00087"></a>00087       <span class="keywordflow">return</span> m_priority &lt; rhs.<a class="code" href="a00021.html#f15f3f366672138894260caad121d517">m_priority</a>; 
<a name="l00088"></a>00088     }
<a name="l00089"></a>00089 
<a name="l00090"></a>00090   };  <span class="comment">// prio_task_func</span>
<a name="l00091"></a>00091 
<a name="l00092"></a>00092 
<a name="l00093"></a>00093 
<a name="l00094"></a>00094  
<a name="l00095"></a>00095 
<a name="l00096"></a>00096 
<a name="l00097"></a>00097 
<a name="l00098"></a>00098 
<a name="l00107"></a><a class="code" href="a00019.html">00107</a>   <span class="keyword">class </span><a class="code" href="a00019.html">looped_task_func</a>
<a name="l00108"></a>00108   {
<a name="l00109"></a>00109   <span class="keyword">private</span>:
<a name="l00110"></a>00110     function0&lt;bool&gt; m_function;   
<a name="l00111"></a>00111     <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> m_break_s;              
<a name="l00112"></a>00112     <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> m_break_ns;             
<a name="l00113"></a>00113 
<a name="l00114"></a>00114   <span class="keyword">public</span>:
<a name="l00115"></a><a class="code" href="a00019.html#0de81e4d23aed52a73fe5ee2d11deaf6">00115</a>     <span class="keyword">typedef</span> <span class="keywordtype">void</span> <a class="code" href="a00019.html#0de81e4d23aed52a73fe5ee2d11deaf6">result_type</a>; 
<a name="l00116"></a>00116 
<a name="l00117"></a>00117   <span class="keyword">public</span>:
<a name="l00122"></a><a class="code" href="a00019.html#46f85a3e3bbde3eaf0e2a186dfbd6e16">00122</a>     <a class="code" href="a00019.html#46f85a3e3bbde3eaf0e2a186dfbd6e16">looped_task_func</a>(function0&lt;bool&gt; <span class="keyword">const</span> &amp; function, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <span class="keyword">const</span> interval = 0)
<a name="l00123"></a>00123       : m_function(function)
<a name="l00124"></a>00124     {
<a name="l00125"></a>00125       m_break_s  = interval / 1000;
<a name="l00126"></a>00126       m_break_ns = (interval - m_break_s * 1000) * 1000 * 1000;
<a name="l00127"></a>00127     }
<a name="l00128"></a>00128 
<a name="l00131"></a><a class="code" href="a00019.html#ab9445cd9c082d77c56eee21175f3133">00131</a>     <span class="keywordtype">void</span> <a class="code" href="a00019.html#ab9445cd9c082d77c56eee21175f3133">operator() </a>(<span class="keywordtype">void</span>)<span class="keyword"> const</span>
<a name="l00132"></a>00132 <span class="keyword">    </span>{
<a name="l00133"></a>00133       <span class="keywordflow">if</span>(m_function)
<a name="l00134"></a>00134       {
<a name="l00135"></a>00135         <span class="keywordflow">if</span>(m_break_s &gt; 0 || m_break_ns &gt; 0)
<a name="l00136"></a>00136         { <span class="comment">// Sleep some time before first execution</span>
<a name="l00137"></a>00137           xtime xt;
<a name="l00138"></a>00138           xtime_get(&amp;xt, TIME_UTC);
<a name="l00139"></a>00139           xt.nsec += m_break_ns;
<a name="l00140"></a>00140           xt.sec += m_break_s;
<a name="l00141"></a>00141           thread::sleep(xt); 
<a name="l00142"></a>00142         }
<a name="l00143"></a>00143 
<a name="l00144"></a>00144         <span class="keywordflow">while</span>(m_function())
<a name="l00145"></a>00145         {
<a name="l00146"></a>00146           <span class="keywordflow">if</span>(m_break_s &gt; 0 || m_break_ns &gt; 0)
<a name="l00147"></a>00147           {
<a name="l00148"></a>00148             xtime xt;
<a name="l00149"></a>00149             xtime_get(&amp;xt, TIME_UTC);
<a name="l00150"></a>00150             xt.nsec += m_break_ns;
<a name="l00151"></a>00151             xt.sec += m_break_s;
<a name="l00152"></a>00152             thread::sleep(xt); 
<a name="l00153"></a>00153           }
<a name="l00154"></a>00154           <span class="keywordflow">else</span>
<a name="l00155"></a>00155           {
<a name="l00156"></a>00156             thread::yield(); <span class="comment">// Be fair to other threads</span>
<a name="l00157"></a>00157           }
<a name="l00158"></a>00158         }
<a name="l00159"></a>00159       }
<a name="l00160"></a>00160     }
<a name="l00161"></a>00161 
<a name="l00162"></a>00162   }; <span class="comment">// looped_task_func</span>
<a name="l00163"></a>00163 
<a name="l00164"></a>00164 
<a name="l00165"></a>00165 } } <span class="comment">// namespace boost::threadpool</span>
<a name="l00166"></a>00166 
<a name="l00167"></a>00167 <span class="preprocessor">#endif // THREADPOOL_TASK_ADAPTERS_HPP_INCLUDED</span>
<a name="l00168"></a>00168 <span class="preprocessor"></span>
</pre></div>    <BR>
    <DIV CLASS="qindex">
      <TABLE BORDER="0" WIDTH="100%">
        <TR>
		  <TD CLASS="tiny" ALIGN="LEFT">Copyright &copy 2005-2007 Philipp Henkel</TD>
          <TD ALIGN="RIGHT">
            <A CLASS="qindex" HREF="../index.html">Overview</A> |
            <A CLASS="qindex" HREF="../reference/annotated.html">Reference</A> |
            <A CLASS="qindex" HREF="../tutorial/intro.html">Tutorial</A> |
            <A CLASS="qindex" HREF="../examples/intro.html">Examples</A> |
            <A CLASS="qindex" HREF="../design/intro.html">Design</A>
          </TD>
        </TR>       
      </TABLE>
    </DIV>  
  </BODY>
</HTML>
